package com.framework.Module.system.menu;

import com.framework.Table.Table;
import com.framerock.baselib.CDate;
import com.framerock.baselib.Rock;
import com.framework.ACore.Model;

import java.util.ArrayList;
import java.util.Map;

public class Tablemenu extends  Table{
   

    protected void initTable() {
        this.tablename   = "menu";
        this.tablenamesm = "菜单管理";

        this.flowsetMap  = Rock.getMap(new String[]{
                "type", "系统",
                "sort", "903",
                "isxt", "1",
        });

        this.addfields("name", "varchar(100)", "", "名称", new String[]{
                "isalign","1",
                "isbt","1",
                "isss","1"
        });
        this.addfields("num", "varchar(50)", "", "编号");
        this.addfields("url", "varchar(300)", "", "地址", new String[]{
                "isalign","1",
                "zdsm","外部就http开头，open:地址新窗口打开，add,模块编号：打开模块录入页"
        });

        this.addfields("pid", "int(11)", "0", "上级ID");

        this.addfields("icons", "varchar(100)", "", "图标", new String[]{
                "iseditlx","1",
                "zdsm","图标可以到下<a href=\"http://www.rockoa.com/icon.html\" target=\"_blank\">查看</a>"
        });
        this.addfields("color", "varchar(20)", "", "颜色", new String[]{
                "islb","0",
        });
        this.addfields("status", "tinyint(1)", "1", "状态@@开启菜单", new String[]{
                "iseditlx","1",
        });
        this.addfields("ispir", "tinyint(1)", "0", "权限@0|平台所有人,1|平台管理员,2|单位所有人,3|单位管理员", new String[]{
                "iseditlx","1",
        });
        this.addfields("ishs", "tinyint(1)", "0", "首显@@PC首页快捷入口显示", new String[]{
                "islb","0",
        });
        this.addfields("sort", "int(11)", "0", "排序号", new String[]{
                "iseditlx","1",
                "ispx","1"
        });
        this.addfields("id", "int(11)", "0", "ID");
        this.addfields("pid", "KEY", "`pid`", "pid索引");
    }

    //创建默认菜单
    private Model mobj;
    protected void firstInsert() {
        mobj      = Model.get(this.nDb, "menu");

        this.addMenuitem("", "个人中心", "admin/geren","user", "0","geren");
        this.addMenuitem(0, "模块管理", "","th-large", "1");
        this.addMenuitem(0, "平台管理", "","cog", "1");
        this.addMenuitem(0, "平台单位", "","road", "1");

        //模块二级
        this.addMenuitem("模块管理", "数据选项", "flow/list?num=option","cog", "1");
        this.addMenuitem("模块管理", "模块列表", "flow/list?num=flowset","th-list", "1");
        this.addMenuitem("模块管理", "模块字段", "flow/list?num=flowelement","check", "1");
        this.addMenuitem("模块管理", "模块条件", "flow/list?num=flowwhere","asterisk", "1");
        this.addMenuitem("模块管理", "模块权限", "flow/list?num=flowauth","lock", "1");
        this.addMenuitem("模块管理", "审核步骤", "flow/list?num=flowcourse","resize-vertical", "1");
        this.addMenuitem("模块管理", "单据通知设置", "flow/list?num=flowtodo","bell", "1");
        this.addMenuitem("模块管理", "单据操作菜单", "flow/list?num=flowmenu","list-ul", "1");


        //二级
        this.addMenuitem("平台管理", "菜单管理", "flow/list?num=menu","list-ul", "1","menu");
        this.addMenuitem("平台管理", "平台用户", "flow/list?num=users","user", "1");
        this.addMenuitem("平台管理", "平台设置", "system/cog","wrench", "1");
        this.addMenuitem("平台管理", "登录记录", "flow/list?num=token","legal", "1");
        this.addMenuitem("平台管理", "计划任务", "flow/list?num=task","time", "1", "task");
        this.addMenuitem("平台管理", "上传的文件", "flow/list?num=file","folder-close-alt", "1", "file");
        this.addMenuitem("平台管理", "单据提醒设置", "flow/list?num=remind&pnum=all","time", "1", "remind");


        this.addMenuitem("平台单位", "单位列表", "flow/list?num=company","", "1","company");
        this.addMenuitem("平台单位", "单位部门", "flow/list?num=dept","sitemap", "1");
        this.addMenuitem("平台单位", "单位用户", "flow/list?num=usera","user", "1");
        this.addMenuitem("平台单位", "单位模块", "flow/list?num=flowunit","th-large", "1");

        //以下是单位管理使用的
        this.addMenuitem("", "个人信息", "admin/geren","user", "2","geren");
        this.addMenuitem("", "单位设置", "company/manage","cog", "3","managehome");

        this.addMenuitem("", "模块设置", "","th-large", "3", "flowunit{cnum}");
        this.addMenuitem("模块设置", "数据选项", "flow/list?num=option","cog", "3","option{cnum}");
        this.addMenuitem("模块设置", "模块应用列表", "flow/list?num=flowunit","th-large", "3", "flowunit{cnum}");
        this.addMenuitem("模块设置", "模块应用权限", "flow/list?num=flowauth","lock", "3", "flowauth{cnum}");
        this.addMenuitem("模块设置", "流程审核步骤", "flow/list?num=flowcourse","resize-vertical", "3", "flowcourse{cnum}");
        this.addMenuitem("模块设置", "单据通知设置", "flow/list?num=flowtodo","bell", "3", "flowtodo{cnum}");
        this.addMenuitem("模块设置", "单据操作记录", "flow/list?num=flowlog","th-list", "3", "flowlog{cnum}");
        this.addMenuitem("模块设置", "单据操作菜单", "flow/list?num=flowmenu","list-ul", "3","flowmenu{cnum}");

        this.addMenuitem(0, "单位管理", "","road", "3");
        this.addMenuitem("单位管理", "部门", "flow/list?num=dept","sitemap", "3","dept{cnum}");
        this.addMenuitem("单位管理", "用户", "flow/list?num=usera","user", "3","usera{cnum}");
        this.addMenuitem("单位管理", "上传的文件", "flow/list?num=file","folder-close-alt", "3", "file{cnum}");

        this.saveMenulist();
    }

    private ArrayList<Map<String, String>> menuList = new ArrayList<Map<String, String>>();
    private Map<String, String> pidArr = Rock.getMap();

    private void addMenuitem(int pid, String name, String url, String icons, String ispir)
    {
        Map<String, String> da =Rock.getMap();
        da.put("name", name);
        da.put("url", url);
        da.put("icons", icons);
        da.put("pid", ""+pid+"");
        da.put("ispir", ispir);
        menuList.add(da);
    }

    private void addMenuitem(String pname, String name, String url, String icons, String ispir, String num)
    {
        Map<String, String> da =Rock.getMap();
        da.put("name", name);
        da.put("url", url);
        da.put("icons", icons);
        da.put("pid", "0");
        da.put("pname", pname);
        da.put("ispir", ispir);
        da.put("num", num);
        menuList.add(da);
    }

    private void addMenuitem(String pname, String name, String url, String icons, String ispir)
    {
        addMenuitem(pname,name,url,icons,ispir,"");
    }

    private int saveMenulist(){
        Map<String, String> da,rs;
        int id = 0;
        String where,pid;
        for(int i=0;i<menuList.size();i++){
            da = menuList.get(i);
            pid= getpid(da);
            da.remove("pname");
            da.put("sort",""+i+"");
            da.put("pid",pid);
            where = "`pid`="+pid+" and `name`='"+da.get("name")+"'";
            rs = mobj.getOne(where);
            if(rs==null){
                id = mobj.insert(da);
            }else{
                da.remove("sort");
                mobj.update(da, where);
                id = Integer.parseInt(rs.get("id"));
            }
            pidArr.put(da.get("name"), ""+id);
        }
        menuList = new ArrayList<Map<String, String>>();
        return id;
    }
    private String getpid(Map<String, String> da){
        String pid = "0",pname = Rock.getMapString(da, "pname");
        if(!Rock.isEmpt(pname)){
            String npid = Rock.getMapString(pidArr, pname);
            if(Rock.isEmpt(npid)) {
                Map<String, String> rs = mobj.getOne("`name`='" + pname + "'");
                if (rs != null) {
                    pid = rs.get("id");
                    pidArr.put(pname, pid);
                }
            }else{
                pid = npid;
            }
        }
        return pid;
    }
}
